//
//@File    :	13.15_dma.cpp
//@Time    :	2022/01/19 14:45:30
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	dma class methods
//

#include "13.14_dma.h"
#include <cstring>

//baseDMA methods
baseDMA::baseDMA(const char *l, int r)
{
    label = new char[std::strlen(l) + 1];
    std::strcpy(label, l);
    rating = r;
}
baseDMA::baseDMA(const baseDMA &rs) //复制构造函数
{
    label = new char[std::strlen(rs.label) + 1];
    std::strcpy(label, rs.label);
    rating = rs.rating;
}
baseDMA::~baseDMA() //析构函数
{
    delete[] label;
}
baseDMA &baseDMA::operator=(const baseDMA &rs) //重载赋值运算符
{
    if (this == &rs)
    {
        return *this;
    }
    delete[] label;
    label = new char[std::strlen(rs.label) + 1];
    std::strcpy(label, rs.label);
    rating = rs.rating;
    return *this;
}
//基类的友元函数
std::ostream &operator<<(std::ostream &os, const baseDMA &rs)
{
    os << "Label: " << rs.label << std::endl;
    os << "rating: " << rs.rating << std::endl;
    return os;
}

//lacksDMA methods
lacksDMA::lacksDMA(const char *c, const char *l, int r) : baseDMA(l, r)
{
    std::strncpy(color,c,39);
    color[39]='\0';
}

lacksDMA::lacksDMA(const char *c, const baseDMA &rs):baseDMA(rs)
{
    std::strncpy(color,c,COL_LEN-1);
    color[COL_LEN-1]='\0';
}
std::ostream &operator<<(std::ostream &os, const lacksDMA &ls)
{
    os<<(const baseDMA &)ls;
    os<<"Color: "<<ls.color<<std::endl;
    return os;
}

//hasDMA methods
hasDMA::hasDMA(const char *s, const char *l, int r):baseDMA(l,r)
{
    style = new char[std::strlen(s)+1];
    std::strcpy(style,s);
}
hasDMA::hasDMA(const char *s, const baseDMA &rs):baseDMA(rs)
{
    style = new char[std::strlen(s)+1];
    std::strcpy(style,s);
}
hasDMA::hasDMA(const hasDMA &hs):baseDMA(hs)//invoke base class copy constructor
{
    style = new char[std::strlen(hs.style)+1];
    std::strcpy(style,hs.style);
}
hasDMA::~hasDMA()
{
    delete [] style;
}
hasDMA& hasDMA::operator=(const hasDMA &hs)
{
     if (this == &hs)
    {
        return *this;
    }
    baseDMA::operator=(hs);//copy base portion
    delete[] style; //prepare for new style
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
    return *this;
}
//友元函数
std::ostream &operator<<(std::ostream &os, const hasDMA &hs)
{
    os<<(const baseDMA &)hs;//强制类型转化,以便匹配类型时能够选择正确的函数
    os<<"Style: "<<hs.style<<std::endl;
    return os;
}